/** 金果摇钱树 */
const {getForJson, postForJson} = require('./jdClient.js');

/** 金果签到 */
function goldTreeLogin() {
    let url = "https://ms.jr.jd.com/gw/generic/uc/h5/m/login";
    let data = {
        "sharePin": "",
        "shareType": 1,
        "channelLV": "",
        "source": 0,
        "riskDeviceParam": "{\"eid\":\"SNL6U7JNW7XSS36WPO3EOHKFKRCINPDSPFJCLDBO5QI22KOUU2TAITOW2X2JDFVH6WC35LELNQBFWQ2HHJ4MEMTF5M\",\"fp\":\"9ce300f0682d177d82b1d3958414c465\",\"sdkToken\":\"\",\"token\":\"J6X5OBBEDSGVSZ2K6OZHXDB23NA2AJIRISN4UNLSQH5DRTDDN664AWOER4PJV6IAXLOPHXDSZJDAQ\",\"jstub\":\"42MJY2Y6U34VJAD3YPQL4WCGTOIFM3BJC4YC5BOU5T3WNZA3OMTJYARH4QACPMNDMBSTEUVPEEQOJ4BJEXMBUNN7T4FUYTTOJVILOBY\"}"
    };
    data = "reqData=" + encodeURIComponent(JSON.stringify(data));
    // data = 'reqData=%7B%22sharePin%22%3A%22%22%2C%22shareType%22%3A1%2C%22channelLV%22%3A%22%22%2C%22source%22%3A0%2C%22riskDeviceParam%22%3A%22%7B%5C%22eid%5C%22%3A%5C%22SNL6U7JNW7XSS36WPO3EOHKFKRCINPDSPFJCLDBO5QI22KOUU2TAITOW2X2JDFVH6WC35LELNQBFWQ2HHJ4MEMTF5M%5C%22%2C%5C%22fp%5C%22%3A%5C%229ce300f0682d177d82b1d3958414c465%5C%22%2C%5C%22sdkToken%5C%22%3A%5C%22%5C%22%2C%5C%22token%5C%22%3A%5C%22J6X5OBBEDSGVSZ2K6OZHXDB23NA2AJIRISN4UNLSQH5DRTDDN664AWOER4PJV6IAXLOPHXDSZJDAQ%5C%22%2C%5C%22jstub%5C%22%3A%5C%2242MJY2Y6U34VJAD3YPQL4WCGTOIFM3BJC4YC5BOU5T3WNZA3OMTJYARH4QACPMNDMBSTEUVPEEQOJ4BJEXMBUNN7T4FUYTTOJVILOBY%5C%22%7D%22%7D';
    let response = postForJson(url, data, {'Referer': 'https://u.jr.jd.com/uc-fe-wxgrowing/moneytree/index/'});
    // log(JSON.stringify(response))
    if (response.resultData && response.resultData.code == 200 && response.resultData.data) {
        let userId = response.resultData.data.userInfo;
        let userToken = response.resultData.data.userToken;
        return {userId: userId, userToken: userToken};
    } else {
        console.log('金果摇钱树登录失败: ' + JSON.stringify(response));
        return null;
    }
}

/** 卖金果 */
function goldTreeSell() {
    let url = "https://ms.jr.jd.com/gw/generic/uc/h5/m/sell";
    let data = {
        "source": 2,
        "riskDeviceParam": "{\"eid\":\"PYQE3CK2HS2D4GHCZLT2DS4BRDEG3KESNZ65BZEYE4LJMO5OGQS2V2SQBY3GTZKAEFPCYDPS6IHJI22WUU2SPYA23M\",\"dt\":\"Xiaomi\",\"ma\":\"\",\"im\":\"865441031402480\",\"os\":\"android\",\"osv\":\"9\",\"ip\":\"117.136.12.225\",\"apid\":\"JDJR-App\",\"ia\":\"\",\"uu\":\"865441031402480-\",\"cv\":\"5.4.40\",\"nt\":\"WIFI\",\"at\":\"3\",\"fp\":\"04b271c0b2121470afacfddff4bec5fc\",\"token\":\"4VAUWOOFTGYB5RTIAVJV3JHTZHRLQIXY4U5NAGNN2YOFOTGSUEUWNSQ2GDPH25J7LEVA66O475H3G\"}"
    };
    data = "reqData=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data, {'Referer': 'https://u.jr.jd.com/uc-fe-wxgrowing/moneytree/index/'});
    if (response.resultData && response.resultData.code == 200 && response.resultData.data) {
        let failAmount = response.resultData.data.failAmount;
        if (failAmount) {
            console.log('金果变卖：不足' + failAmount + '金果');
        } else {
            console.log('金果变卖金币成功');
        }
    } else {
        console.log('金果变卖失败: ' + JSON.stringify(response));
    }
}

/** 收金果摇钱树 */
function goldTreeGet() {
    let userInfo = goldTreeLogin();
    if (!userInfo) {
        console.log('获取用户信息失败，无法收金果');
        return;
    }
    let url = "https://ms.jr.jd.com/gw/generic/uc/h5/m/harvest";
    let data = {
        "source": 2,
        "sharePin": "",
        "userId": userInfo.userId,
        "userToken": userInfo.userToken,
        "shareType": 1,
        "channel": "",
        "riskDeviceParam": "{\"eid\":\"PYQE3CK2HS2D4GHCZLT2DS4BRDEG3KESNZ65BZEYE4LJMO5OGQS2V2SQBY3GTZKAEFPCYDPS6IHJI22WUU2SPYA23M\",\"dt\":\"Xiaomi\",\"ma\":\"\",\"im\":\"865441031402480\",\"os\":\"android\",\"osv\":\"9\",\"ip\":\"120.230.131.147\",\"apid\":\"JDJR-App\",\"ia\":\"\",\"uu\":\"865441031402480-\",\"cv\":\"5.4.40\",\"nt\":\"WIFI\",\"at\":\"3\",\"fp\":\"dd34f96a2fec53813be7df77c73d8aeb\",\"token\":\"IIBMHLIXPTOZVO3WRMINHWJPWFMAHHMRPOYOFMYWJU42UQG3XRUUVFPWGC7PYKLVYFLP2ZTOKMBKA\"}"
    };
    data = "reqData=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data, {'Referer': 'https://u.jr.jd.com/uc-fe-wxgrowing/moneytree/index/'});
    if (response.resultData && response.resultData.code == 200 && response.resultData.data && response.resultData.data.treeInfo) {
        let treeInfo = response.resultData.data.treeInfo;
        let upgrade = response.resultData.data.upgrade;
        console.log('收金果摇钱树: 现有金果%s，%s升级', treeInfo.fruit, (upgrade ? '需要' : '不需要'));
        goldTreeSell();
    } else {
        console.log('收金果摇钱树失败: ' + JSON.stringify(response));
    }
}


/** 金果树签到 */
function goldTreeSign() {
    let url = "https://ms.jr.jd.com/gw/generic/uc/h5/m/signOne";
    let data = {
        "source": 0,
        "signDay": 2,
        "riskDeviceParam": "{\"eid\":\"SNL6U7JNW7XSS36WPO3EOHKFKRCINPDSPFJCLDBO5QI22KOUU2TAITOW2X2JDFVH6WC35LELNQBFWQ2HHJ4MEMTF5M\",\"fp\":\"9ce300f0682d177d82b1d3958414c465\",\"sdkToken\":\"\",\"token\":\"J6X5OBBEDSGVSZ2K6OZHXDB23NA2AJIRISN4UNLSQH5DRTDDN664AWOER4PJV6IAXLOPHXDSZJDAQ\",\"jstub\":\"42MJY2Y6U34VJAD3YPQL4WCGTOIFM3BJC4YC5BOU5T3WNZA3OMTJYARH4QACPMNDMBSTEUVPEEQOJ4BJEXMBUNN7T4FUYTTOJVILOBY\"}"
    }
    data = "reqData=" + encodeURIComponent(JSON.stringify(data));
    let response = postForJson(url, data, {'Referer': 'https://u.jr.jd.com/uc-fe-wxgrowing/moneytree/index/'});
    if (response.resultData && response.resultData.code == 200 && response.resultData.data) {
        if (response.resultData.data.result) {
            console.log('金果摇钱树：签到成功');
        } else {
            // log(JSON.stringify(response))
            console.log('金果摇钱树：未到签到时间');
        }
    } else {
        console.log('金果摇钱树签到失败: ' + JSON.stringify(response));
    }
}

/** 金果摇钱树分享助力 */
function goldTreeShare() {
    let url = "https://jrmfp.jr.jd.com/npvuv";
    let data = {
        "bid": "jrjssdk1001",
        "param_json": {
            "v": "1.0.0",
            "s": "Android",
            "a": "JDJRAPP",
            "c": [
                "clientVersion=5.4.40&"
            ],
            "u": "https%3A%2F%2Fu.jr.jd.com%2Fuc-fe-wxgrowing%2Fmoneytree%2Findex%2F%3FsourceID%3D214%26shareType%3D1%26sharePin%3DidvJlCFxOswnkBBiyKh1gcAdoUJQ3Dik%26jrcontainer%3Dh5%26jrlogin%3Dtrue%26jrcloseweb%3Dfalse%26jrwallet%3Dfalse%26jrxviewtype%3Dfalse%26jrgobackrefresh%3Dfalse",
            "t": "金果摇钱树",
            "l": ""
        }
    };
    data = JSON.stringify(data);
    let response = postForJson(url, data);
    if (response.resultCode == 0) {
        console.log('金果摇钱树：助力成功');
    } else {
        console.log('金果摇钱树助力失败: ' + JSON.stringify(response));
    }
}

function run() {
    console.log("=== 金果摇钱树开始 ===");
    // 金果签到
    goldTreeSign();
    // 金果分享助力
    goldTreeShare();
    // 收金果
    goldTreeGet();
    console.log("=== 金果摇钱树结束 ===");
}

module.exports = {
    run: run
};